package com.duke.shaking;

import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

public abstract class TitleActivity extends BaseActivity {
	
	protected static final long TITLE_BTN_EXIST = 0x01;
	protected static final long TITLE_BTN_BACK =  0x02;
	protected Button btnLeft;
	protected Button btnRight;
	protected TextView txtTitle;
	
	protected boolean leftBtnIsBack;
	abstract protected int getLayoutId();
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(getLayoutId());
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, 
        		R.layout.title);
        onTitleInit();
	}
	
	abstract protected void onTitleInit();
	
	protected void initTitle(long leftBtnStyle, int leftBtnText,
			long rightBtnStyle, int rightBtnText,
			int titleText
			) {
		btnLeft = (Button)findViewById(R.id.btn_left);
		btnRight = (Button)findViewById(R.id.btn_right);
		txtTitle = (TextView)findViewById(R.id.txt_title);
		if ((leftBtnStyle & TITLE_BTN_EXIST) != 0) {
			if (btnLeft != null) {
				btnLeft.setVisibility(View.VISIBLE);
				btnLeft.setText(leftBtnText);
				if ((leftBtnStyle & TITLE_BTN_BACK) != 0) {
					btnLeft.setBackgroundResource(R.drawable.back);
					leftBtnIsBack = true;
				}
				else {
					btnLeft.setBackgroundResource(R.drawable.go);
				}
			}
		}
		else {
			if (btnLeft != null) {
				btnLeft.setVisibility(View.INVISIBLE);
			}
		}
		if ((rightBtnStyle & TITLE_BTN_EXIST) != 0) {
			if (btnRight != null) {
				btnRight.setVisibility(View.VISIBLE);
				btnRight.setText(rightBtnText);
				btnRight.setBackgroundResource(R.drawable.go);
			}
		}
		else {
			if (btnRight != null) {
				btnRight.setVisibility(View.INVISIBLE);
			}
		}
		
		if (titleText > 0) {
			txtTitle.setText(titleText);
		}
	}
	
	public void onLeftBtn(View v) {
		if (leftBtnIsBack) {
			finish();
		}
	}
	
	public void onRightBtn(View v) {
		
	}
	
}
